package com.example.demo.zaqizabatest.leetcode.day2;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 示例:
 * <p>
 * 输入: 1->2->3->4->5->NULL
 * 输出: 5->4->3->2->1->NULL
 */
public class Solution2 {

    public ListNode reverseList(ListNode head) {
        if (head != null) {
            List<Integer> list = new ArrayList<>();
            List<ListNode> nodes = new ArrayList<>();
            list.add(head.val);
            ListNode nextNode = head.next;
            while (nextNode != null) {
                list.add(nextNode.val);
                nextNode = nextNode.next;
            }
            Collections.reverse(list);
            if (list.size() == 1) {
                return new ListNode(list.get(0));
            }
            for (int i = 0; i < list.size(); i++) {
                nodes.add(new ListNode(list.get(i)));
            }
            for (int i = 0; i < nodes.size() - 1; i++) {
                nodes.get(i).next = nodes.get(i + 1);
            }
            return nodes.get(0);
        }
        return null;
    }

    public class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
        }
    }
}
